//
//  PhotoBrowserController.swift
//  ImageBrowse
//
//  Created by zs on 16/6/19.
//  Copyright © 2016年 zs. All rights reserved.
//

import UIKit
let ID = "collectionCell"
class PhotoBrowserController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate{
    
    // MARK:- 定义属性
    var shops : [Shop]?
    var indexPath : NSIndexPath?
    
    
    
    //懒加载创建collectionView
    
    private lazy var collectionView : UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: PhotoViewLayout())
    
    private lazy var leftBtn : UIButton = UIButton(title: "返 回", bgColor: UIColor.darkGrayColor(), fontSize: 14)
    private lazy var rightBtn : UIButton = UIButton(title: "保 存", bgColor: UIColor.darkGrayColor(), fontSize: 14)
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.frame.size.width += 15
        SetUpUI()
        
        if let indexPath = indexPath {
            collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        }
    }

}

extension PhotoBrowserController {
    private func SetUpUI(){
        
        //添加子控件
        view.addSubview(collectionView)
        view.addSubview(leftBtn)
        view.addSubview(rightBtn)
        
        collectionView.frame = view.bounds
        collectionView.dataSource = self
        collectionView.delegate = self
        // MARK:- 注册cell
        collectionView.registerClass(PhotoBrowserCollectionViewCell.self, forCellWithReuseIdentifier: ID)
        
        let Margin : CGFloat = 20
        let BtnW : CGFloat = 90
        let BtnH : CGFloat = 32
        //计算X, Y
        let leftBtnX = Margin
        let rightBtnX = UIScreen.mainScreen().bounds.width - Margin - BtnW
        
        let BtnY = UIScreen.mainScreen().bounds.height - Margin - BtnH
        leftBtn.frame = CGRect(x: leftBtnX, y: BtnY, width: BtnW, height: BtnH)
        rightBtn.frame = CGRect(x: rightBtnX, y: BtnY, width: BtnW, height: BtnH)
        
        leftBtn.addTarget(self, action:#selector(leftBtnClick), forControlEvents: .TouchUpInside)
        
        rightBtn.addTarget(self, action:#selector(rightBtnClick), forControlEvents: .TouchUpInside)
      }
    
}

extension PhotoBrowserController {

  @objc private func leftBtnClick(){
        dismissViewControllerAnimated(true, completion: nil)
    
    }
    @objc private func rightBtnClick(){
        let cell = collectionView.visibleCells()[0] as! PhotoBrowserCollectionViewCell
        
        let image = cell.imageView.image
        
       UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
    
    }
}

extension PhotoBrowserController {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return shops?.count ?? 0
    }
 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ID, forIndexPath: indexPath) as! PhotoBrowserCollectionViewCell
        
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.magentaColor() : UIColor.blueColor()
        cell.shop = shops![indexPath.item]
        
        return cell
        
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        leftBtnClick()
    }

}